package com.zxk.service.impl;

import com.sun.jna.platform.win32.WinDef;
import com.zxk.app.ContextHolder;
import com.zxk.app.dto.TaskMappingDto;
import com.zxk.constant.PicConstants;
import com.zxk.enums.Coord;
import com.zxk.service.AbstractInstanceTask;
import com.zxk.service.AbstractMyTask;
import com.zxk.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 侠士副本1-侠士副本
 *
 * @author: 舟小亢
 * @date: 2023-08-31 23:03
 */
public class HardInstance1TaskImpl extends AbstractInstanceTask {

    private static Logger LOG = LoggerFactory.getLogger(HardInstance1TaskImpl.class);

    public HardInstance1TaskImpl(WinDef.HWND hwnd_0, WinDef.HWND hwnd_1, WinDef.HWND hwnd_2) {
        super(hwnd_0, hwnd_1, hwnd_2);
    }

    @Override
    public String getTaskName() throws Exception {
        return TaskMappingDto.HARD_INSTANCE_1;
    }

    @Override
    public boolean goHome() throws Exception {
        if (!super.goHome()) {
            if (getTaskName().equals(ContextHolder.getLastInstanceTaskName())) {
                afterFinishTask();
            }
            return false;
        }
        return true;
    }


    @Override
    public boolean execute() throws Exception {
        if (!executeResult()) {
            if (getTaskName().equals(ContextHolder.getLastInstanceTaskName())) {
                afterFinishTask();
            }
            return false;
        }
        return true;
    }

    private boolean executeResult() throws Exception {
        LOG.warn("【侠士副本1】开始！");

        // 切换组合2
        if (!changeAssistantTeam(Coord.ASSISTANT_FRIEND_2)) {
            LOG.info("【侠士副本1】切换助战组合[2]失败！");
        }
        LOG.info("【侠士副本1】切换助战组合[2]成功！");

        // 点击当前地图
        MouseUtil.lClickRandom(hwnd_1, Coord.CURRENT_MAP_XY.getX(), Coord.CURRENT_MAP_XY.getY());

        // 点击百晓仙子
        ThreadUtil.sleep(1500);
        MouseUtil.lClickRandom(hwnd_1,Coord.BAI_XIAO_XIAN_ZI_XY.getX(),Coord.BAI_XIAO_XIAN_ZI_XY.getY());

        // 点击接受任务
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.副本任务_接受任务)) {
            return false;
        }

        // 点击侠士副本
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.副本任务_侠士副本)) {
            return false;
        }

        // 点击进入侠士副本
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.副本任务_进入)) {
            return false;
        }

        return true;
    }


    @Override
    public boolean checkTask() throws Exception {
        try {
            return checkTaskResult();
        } finally {
            if (getTaskName().equals(ContextHolder.getLastInstanceTaskName())) {
                afterFinishTask();
            }
        }
    }

    private boolean checkTaskResult() throws Exception {
        // 循环点击跳过剧情 战斗(战斗确认)
        for (int i = 0; i < 30; i++) {
            LOG.warn("【侠士副本1】循环检查第{}次！", i + 1);
            // 战斗失败，检查活动
            if (PixelPicUtil.findPic(hwnd_0, PicConstants.公共_活动, 3).isFinded()) {
                LOG.error("【侠士副本1】找到活动,休眠后再次确认是否退出");
                ThreadUtil.sleep(15000);
                // 双重确认
                if (PixelPicUtil.findPic(hwnd_0, PicConstants.公共_活动, 3).isFinded()) {
                    LOG.error("【侠士副本1】战斗失败！退出");
                    return false;
                }
            }

            // 点击跳过剧情
            FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.副本任务_跳过剧情, 2);

            // 点击战斗
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.副本任务_战斗, 3).isFinded()) {
                if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.副本任务_战斗确认, 3).isFinded()) {
                    FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.副本任务_战斗确认, 2);
                } else {
                    FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.副本任务_战斗, 2);
                }
            }

            // 查找战斗确认（可能不存在该环节）
            FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.副本任务_战斗确认, 2);

            // 退出机制
            if (FuzzyPicUtil.findPicM(hwnd_0, new String[]{PicConstants.副本任务_副本结束点击继续, PicConstants.副本任务_副本结束退出}, 2).isFinded()) {
                // 找到完成战斗后结束
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.副本任务_副本结束点击继续, 3);
                // 点击退出
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.副本任务_副本结束退出, 3);
                break;
            }

            // 如果是战斗中，则睡眠
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.副本任务_战斗中, 2).isFinded()) {
                LOG.warn("【侠士副本1】战斗中，休眠10秒！");
                ThreadUtil.sleep(10000);
            }
        }
        LOG.warn("【侠士副本1】完成！");

        return true;
    }

}
